<template>
  <el-card class="container-card">
    <el-form inline :disabled="disabled">
      <el-form-item label="一级分类" class="form-category-item">
        <el-select
          :value="category1Id"
          placeholder="请选择一级分类"
          @change="getCategory2List"
        >
          <el-option
            v-for="category1 in category1List"
            :key="category1.id"
            :value="category1.id"
            :label="category1.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" class="form-category-item">
        <el-select
          :value="category2Id"
          @change="getCategory3List"
          placeholder="请选择二级分类"
        >
          <el-option
            v-for="category2 in category2List"
            :key="category2.id"
            :value="category2.id"
            :label="category2.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类" class="form-category-item">
        <el-select
          :value="category3Id"
          @change="SET_CATEGORY3ID"
          placeholder="请选择三级分类"
        >
          <el-option
            v-for="category3 in category3List"
            :key="category3.id"
            :value="category3.id"
            :label="category3.name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  name: "CategorySelector",
  mounted() {
    this.getCategory1List();
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState("category", [
      "category1Id",
      "category2Id",
      "category3Id",
      "category1List",
      "category2List",
      "category3List"
    ])
  },
  methods: {
    ...mapActions("category", [
      "getCategory1List",
      "getCategory2List",
      "getCategory3List"
    ]),
    ...mapMutations("category", ["SET_CATEGORY3ID"])
  }
};
</script>

<style scoped>
.form-category-item {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>
